package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyMqttConfigAddBo;
import com.ruoyi.system.bo.ZsyyMqttConfigQueryBo;
import com.ruoyi.system.bo.ZsyyMqttConfigEditBo;
import com.ruoyi.system.domain.ZsyyMqttConfig;
import com.ruoyi.system.mapper.ZsyyMqttConfigMapper;
import com.ruoyi.system.vo.ZsyyMqttConfigVo;
import com.ruoyi.system.service.IZsyyMqttConfigService;
import com.ruoyi.system.mapper.ZsyyMqttConfigMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 黑盒子mqtt配置Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyMqttConfigServiceImpl extends ServicePlusImpl<ZsyyMqttConfigMapper, ZsyyMqttConfig> implements IZsyyMqttConfigService {
    @Autowired
    private ZsyyMqttConfigMapper iZsyyMqttConfigMapper;

    @Override
    public ZsyyMqttConfigVo queryById(Integer id){
        return getVoById(id, ZsyyMqttConfigVo.class);
    }

    @Override
    public TableDataInfo<ZsyyMqttConfigVo> queryPageList(ZsyyMqttConfigQueryBo bo) {
        PagePlus<ZsyyMqttConfig, ZsyyMqttConfigVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyMqttConfigVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyMqttConfigVo> queryList(ZsyyMqttConfigQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyMqttConfigVo.class);
    }

    private LambdaQueryWrapper<ZsyyMqttConfig> buildQueryWrapper(ZsyyMqttConfigQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyMqttConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), ZsyyMqttConfig::getName, bo.getName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyMqttConfigAddBo bo) {
        ZsyyMqttConfig add = BeanUtil.toBean(bo, ZsyyMqttConfig.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyMqttConfigEditBo bo) {
        ZsyyMqttConfig update = BeanUtil.toBean(bo, ZsyyMqttConfig.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyMqttConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
